#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/5/14 15:42
# @Author  : huidong.bai
# @File    : BigJsonGenerator.py
# @Software: PyCharm
# @Mail    : MasterBai2018@outlook.com

import json
import uuid
import random
import time
from copy import deepcopy


random_id = 1000000
random_level = 500


def get_timestamp():
    return int(time.time() * 1000)


def get_random_level():
    global random_level
    random_temp = random_level + 1
    random_level = random_temp
    return random_temp


def get_random_id():
    global random_id
    random_str = str(random_id)
    random_id += 1
    return random_str


page_skeleton_base = {
    "source": "TSA",
    "type": "SetVoiceControlledInfo",
    "et_id": str(uuid.uuid4()),
    "mts": get_timestamp(),
    "data": {
        "HMI": []
    }
}


def get_random_synonyms(text, synonyms_header):
    combined_text = []
    num_combinations = random.randint(0, 4)  # 随机取0到3次
    for _ in range(num_combinations):
        random_header = random.choice(synonyms_header)
        combined_text.append(random_header + text)
    return combined_text


def get_random_keyword():
    files = ["data/random_data/random_data.txt",
             "data/random_data/random_eng.txt",
             "data/random_data/random_number.txt",
             "data/random_data/random_poi.txt",
             "data/random_data/random_punc.txt"
             ]
    file_contents = []

    # 读取文件内容
    for file_name in files:
        with open(file_name, 'r') as file:
            file_contents.append(file.readlines())

    random_string = ""

    # 随机选择文件和行数，并拼接字符串
    for _ in range(5):
        random_file_index = random.randint(0, len(files) - 1)
        random_line_index = random.randint(0, len(file_contents[random_file_index]) - 1)
        random_string += file_contents[random_file_index][random_line_index].strip()

    synonyms_header = ["帮我", "点击", "给我点击", "切换"]
    keyword = random_string.strip()
    synonyms_list = get_random_synonyms(keyword, synonyms_header)
    return synonyms_list, keyword


def generate_hmi_info_atom(n):
    hmi_info_atoms = []
    for _ in range(n):
        hmi_info_atom = {
            "HMIInfo": [],
            "appName": "com.random.hmi.appName." + str(uuid.uuid4())[:4],
            "channelMask": 63,
            "pageName": "com.random.hmi.pageName." + str(uuid.uuid4())[:4],
            "screenId": 0
        }
        hmi_info_atoms.append(hmi_info_atom)
    return hmi_info_atoms


def generate_element_info(n):
    elements = []
    for _ in range(n):
        e_id = get_random_id()
        e_level = get_random_level()
        synonyms, keyword = get_random_keyword()
        element_info_atom = {
            "elements": {
                "name": [
                    {
                        "id": e_id,
                        "keyword": keyword,
                        "synonyms": synonyms
                    }
                ]
            },
            "hmiId": e_id,
            "level": e_level,
            "matchType": "default",
            "type": "customBtn"
        }
        elements.append(element_info_atom)
    return elements


def generate_file(sums=50, path="bigdata.json"):
    page_skeleton = deepcopy(page_skeleton_base)
    # 1、生成100到200之间随机数个数的hmi_info_atom列表
    random_number = random.randint(sums, sums+10)
    hmi_info_list = generate_hmi_info_atom(random_number)

    for hmi in hmi_info_list:
        # 2、生成200到300之间随机数个数的elements列表
        random_number = random.randint(sums, sums+10)
        elements = generate_element_info(random_number)
        hmi['HMIInfo'] = elements

    page_skeleton['data']['HMI'] = hmi_info_list
    print(page_skeleton)

    json_data = json.dumps(page_skeleton, ensure_ascii=False, separators=(',', ':'))

    # 将JSON数据写入文件
    with open(path, "w") as json_file:
        json_file.write(json_data)


def generate_json(sums=50):
    page_skeleton = deepcopy(page_skeleton_base)
    # 1、生成100到200之间随机数个数的hmi_info_atom列表
    random_number = random.randint(sums, sums + 10)
    hmi_info_list = generate_hmi_info_atom(random_number)

    for hmi in hmi_info_list:
        # 2、生成200到300之间随机数个数的elements列表
        random_number = random.randint(sums, sums + 10)
        elements = generate_element_info(random_number)
        hmi['HMIInfo'] = elements

    page_skeleton['data']['HMI'] = hmi_info_list
    return page_skeleton


if __name__ == '__main__':
    generate_file(150, path="bigdata_3.json")
